package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.model.Result2
import com.cxh.rmsq.net.self.AllViewModel
import com.cxh.rmsq.ui.activity.LoginActivity
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.utils.MyUtils
import com.cxh.rmsq.viewmodel.UserInfoViewModel
import com.hjq.shape.view.ShapeEditText
import io.rong.callkit.util.SPUtils
import io.rong.imkit.feature.resend.ResendManager

/**
 * 忘记密码
 */
class ForgetPwdActivity : TitleBaseActivity/*<AllViewModel,ActivityForgetPwdBinding>*/() {
    private val tvConfirm: TextView by lazy { findViewById<TextView>(R.id.tv_confirm) }
    private val etNewPassword: ShapeEditText by lazy { findViewById<ShapeEditText>(R.id.et_new_password) }
    private val etConfirmPassword: ShapeEditText by lazy { findViewById<ShapeEditText>(R.id.et_confirm_password) }
    private var userInfoViewModel: UserInfoViewModel? = null
    private var viewModel: AllViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_forget_pwd)
        findViewById<RelativeLayout>(R.id.rl_title).setPadding(0, 0, 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.white))
        BarUtils.setStatusBarLightMode(this, true)

        initView()
        initData()
    }

     fun initView() {
        userInfoViewModel =
            ViewModelProviders.of(this).get<UserInfoViewModel>(UserInfoViewModel::class.java)
//        viewModel =
//            ViewModelProviders.of(this).get<AllViewModel>(AllViewModel::class.java)
//        ivBack.setOnClickListener {
//            finish()
//        }
        if ("2" == intent.getStringExtra("isEdit")) {//2 忘记密码  1 修改密码
            titleBar.tvTitle.text = "忘记密码"
        } else {
            titleBar.tvTitle.text = "修改密码"
        }

         userInfoViewModel!!.changePwd()
             .observe(this@ForgetPwdActivity) { resource: Result2<String?> ->
                 if (resource.code == 200 && resource.data != null) {
                     finishLogout(false)
                     showToast("修改密码成功")
                 } else {
                     showToast(resource.msg)

                 }
             }
    }

    fun initData() {
        var regex = "^(?=.*[0-9])(?=.*[a-zA-Z]).+$"
        tvConfirm.setOnClickListener {
            if (!MyUtils.isFastClick()) {
                if (TextUtils.isEmpty(etNewPassword.text.toString().trim())) {
                    showToast("新密码不能为空")
                    return@setOnClickListener
                }
                if (TextUtils.isEmpty(etConfirmPassword.text.toString().trim())) {
                    showToast("确认密码不能为空")
                    return@setOnClickListener
                }
                if (etNewPassword.text.toString().trim().length in 16..6) {
                    showToast("请输入6～16位密码")
                    return@setOnClickListener
                }
                if (etConfirmPassword.text.toString().trim().length in 16..6) {
                    showToast("请输入6～16位密码")
                    return@setOnClickListener
                }

                if (!etNewPassword.text.toString().trim().matches(regex.toRegex())) {
                    showToast("密码需包含字母与数字")
                    return@setOnClickListener
                }
                if (!etConfirmPassword.text.toString().trim().matches(regex.toRegex())) {
                    showToast("密码需包含字母与数字")
                    return@setOnClickListener
                }
                if (etNewPassword.text.toString().trim() != etConfirmPassword.text.toString().trim()) {
                    showToast("两次密码输入不一致")
                    return@setOnClickListener
                }

                var pwd = etNewPassword.text.toString().trim()
//                viewModel.changePwd(pwd)
//                viewModel.changePwd.observe(this) {
//                    if (it.code == 200) {
//                        showCustomizeToast("修改成功")
//                        finishLogout(false)
//                    } else {
//                        showCustomizeToast("密码需包含字母，数字跟特殊字符")
//                    }
//                }

                userInfoViewModel!!.changePwd(pwd)

            }
        }
    }

    private fun finishLogout(isAccountDeleted: Boolean) {
        ResendManager.getInstance().removeAllResendMessage()
        logout(isAccountDeleted)
        // 通知退出
        sendLogoutNotify()
        SPUtils.clear(applicationContext)
        com.blankj.utilcode.util.SPUtils.getInstance().clear()
        startActivity(Intent(this, LoginActivity::class.java))
        finish()
//        ActivityCollector.finishActivity(SettingActivity::class.java)
    }

    private fun logout(isAccountDeleted: Boolean) {
        if (userInfoViewModel != null) {
            if (isAccountDeleted) {
                userInfoViewModel!!.accountDelete()
            } else {
                userInfoViewModel!!.logout()
            }
        }
    }

}